from collections import deque

n = 9
r = [0] * n
c = [0] * n
b = [0] * n

nums = []
# nums.append("041008009")
# nums.append("000001300")
# nums.append("500600010")

# nums.append("003010507")
# nums.append("020000030")
# nums.append("807090600")

# nums.append("090003006")
# nums.append("006100000")
# nums.append("700200980")

# str = "041008009,000001300,500600010,003010507,020000030,807090600,090003006,006100000,700200980"
# str = "003005607,040000005,700380201,090700000,005139700,000002080,304057006,500000000,608910400"
# str = "001309700,700500006,560000018,045693070,000050000,070482050,630000081,200000009,004901300"
# str = "050000090,000104060,904030802,071902580,800000006,095407230,406000108,000508000,010000020"
# str = "074680000,000002600,008090000,800000010,951000274,020004005,000040100,009500000,000018490"
# str = "074680000000002600008090000800000010951000274020004005000040100009500000000018490"
# str = "000506000010070900040809502200600415130000067060008003601705020007020080000904000"
# str = "031748906000003000800060041004000000600070002002000800170080009000600000409312050"
# str = "090000007040003000120050390800004006730000041200800005013670084000305070900000050"
# str = "000070200006820040270405030003704810820010097017502400080906001000058900005030000"
# str = "800002060030004050000960000000071800160000032004250000000598000010600080070140009"
# str = "650000490400700600000603570040065000010090050000230040026908000000006000073000061"
# print(len(str))
# str = "000002560008000047350407000030090801000000000604070052000001025780000100021900000"
str = (
<<<<<<< Updated upstream
    "160000370000600000000819620040032000050094010000560040075306000000007000094000056"
=======
    "000003120250700040708000600900080060000314000320070005002040806080002014047600000"
>>>>>>> Stashed changes
)

# nums = str.split(',')
nums = []
for i in range(9):
    row = []
    for j in range(9):
        row.append(str[i * 9 + j])
    nums.append(row)

grid = [[0] * n for _ in range(n)]

for i in range(n):
    for j in range(n):
        grid[i][j] = ord(nums[i][j]) - ord("0")

for i in range(n):
    for j in range(n):
        m = grid[i][j]
        if m != 0:
            p = 1 << m
            r[i] |= p
            c[j] |= p
            b[i // 3 * 3 + j // 3] |= p


def f(idx):
    if idx == n * n:
        return True
    x, y = idx // n, idx % n
    if grid[x][y] != 0:
        return f(idx + 1)

    for i in range(1, n + 1):
        p = 1 << i
        z = x // 3 * 3 + y // 3
        if r[x] & p == 0 and c[y] & p == 0 and b[z] & p == 0:
            grid[x][y] = i
            r[x] |= p
            c[y] |= p
            b[z] |= p
            if f(idx + 1):
                return True
            grid[x][y] = 0
            r[x] ^= p
            c[y] ^= p
            b[z] ^= p
    return False


print(f(0))

# for row in grid:
#     print(row)

ans = [[[0] * 3 for _ in range(3)] for _ in range(9)]
for i in range(n):
    j = 0
    for ri in range(i // 3 * 3, (i // 3 + 1) * 3):
        k = 0
        for ci in range(i % 3 * 3, i % 3 * 3 + 3):
            ans[i][j][k] = grid[ri][ci]
            k += 1
        j += 1

# for block in ans:
#     for row in block:
#         print(row)
#     print("----")

for i in range(n):
    print(
        ans[i // 3 * 3][i % 3],
        "  ",
        ans[i // 3 * 3 + 1][i % 3],
        "  ",
        ans[i // 3 * 3 + 2][i % 3],
    )
    if i % 3 == 2:
        print(" ")
